﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickApp.Core;
using EfSearchModel.Model;
using QuickApp.MVCControl;
using Resto.CostControl.Core;
using QuickApp.MVCControl.Form;
using Resto.CostControl.Core.Models;

namespace Resto.CostControl.Web.Controllers
{
    public class MaterialPurchasePlanController : Controller
    {
        private readonly IRepository<MaterialPurchasePlan> _RepoItem;
        private readonly IUnitOfWork _UnitOfWork;

        public MaterialPurchasePlanController(IRepository<MaterialPurchasePlan> repoDM, UnitOfWork unitOfWork)
        {
            _RepoItem = repoDM;
            _UnitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult List(DWZPageInfo pageInfo, DWZQueryModel where)
        {
            //where.AddItem("Week", CommonController.GetWeekOfYear(DateTime.Now).ToString());
            var list = _RepoItem.GetPageable(pageInfo.ToPageInfo(), where);
            var pageList = list.Page.ToList();
            foreach (var item in pageList)
            {
                item.TotalWeight = item.TotalWeight / 500;
                item.PurchaseWeight = item.PurchaseWeight / 500;
                item.ActualSorage = item.ActualSorage / 500;
                item.SafeStorage = item.SafeStorage / 500;
            }
            var fdata = pageInfo.ToFlexiGridData<MaterialPurchasePlan>(pageList, list.Total);
            return Json(fdata);
        }

        public ActionResult SetectType()
        {
            var types = (from plan in _RepoItem.FindAll()
                         select new PurchaseTypeViewModel
                         {
                             SupplierId = plan.SupplierId,
                             SupplierName = plan.SupplierName,
                             Date = plan.Date,
                         }).Distinct();
            return View(types);
        }
    }

    public class PurchaseTypeViewModel
    {
        public string SupplierId { get; set; }
        public string SupplierName { get; set; }
        public DateTime Date { get; set; }
        public string DateString { get { return Date.ToString("yyyy-MM-dd"); } }
    }
}
